package com.hongtao3.hsync.ui;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Stack;

import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JToolBar;
import javax.swing.ScrollPaneConstants;

import com.hongtao3.hsync.Main;


public class MainFrame extends JFrame {
  public MainFrame(){
	  setTitle("hsync client");
	  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	  setIconImage(Main.getTrayImage());
	  init();
	  validate();
	  setVisible(true);
	  splitPane.setDividerLocation(0.5);
	  splitPaneV.setDividerLocation(0.8);
  }
  
  private void init(){
	  con=getContentPane();
	  setBounds(100, 100, 800, 600);
	  
	  con.setLayout(new BorderLayout());
	  addToolbar();
	  splitPane=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,getLocalPanel(),getRemotePanel());
	  splitPaneV=new JSplitPane(JSplitPane.VERTICAL_SPLIT,splitPane,getLogPanel());
	  con.add(splitPaneV,BorderLayout.CENTER);
     
	  addComponentListener(new ComponentListener() {
			@Override
			public void componentShown(ComponentEvent e) {
			}
			@Override
			public void componentResized(ComponentEvent e) {
				splitPane.setDividerLocation(0.5);
				splitPaneV.setDividerLocation(0.8);
			}
			
			@Override
			public void componentMoved(ComponentEvent e) {
				
			}
			@Override
			public void componentHidden(ComponentEvent e) {
			}
		});
  }
  
  public LocalPanel getLocalPanel(){
	  if(localPanel==null)localPanel=new LocalPanel();
	  return localPanel;
  }
  
  public RemotePanel getRemotePanel(){
	  if(remotePanel==null)remotePanel=new RemotePanel();
	  return remotePanel;
  }

  public static void main(String[] args){
	  mainFrame=new MainFrame();
	  mainFrame.getLocalPanel().showPath("/home/duwei/");
  }
  
  private void addToolbar(){
	  JToolBar bar=new JToolBar();
	  bar.setFloatable(false);
	  JButton addBt=new JButton("add");
	  bar.add(addBt);
	  addBt.addActionListener(new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent arg0) {
             new ServerConfigDialog(null, "config");			
		}
	});
	  con.add(bar,BorderLayout.NORTH);
  }
  
  private JScrollPane getLogPanel(){
	  logEditorPanel=new JEditorPane();
	  logEditorPanel.setEditable(false);
	  logEditorPanel.setContentType("text/html");
	  final JScrollPane p=new JScrollPane(logEditorPanel);
	  p.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
	  p.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
	  final JPopupMenu pop=new JPopupMenu();
	  JMenuItem clearItem=new JMenuItem("clear");
	  pop.add(clearItem);
	  clearItem.addActionListener(new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent arg0) {
           logEditorPanel.setText("");			
		}
	  });
	  p.add(pop);
	  logEditorPanel.addMouseListener(new MouseAdapter() {
		@Override
		public void mousePressed(MouseEvent e) {
			super.mousePressed(e);
			if(e.getButton()==3){
				pop.show(p,e.getX(),e.getY());
			}
		}
	   });
	  return p;
  }
  
  public void showLog(String log){
	  logs.push(log);
	  if(logs.size()>100){
		  logs.pop();
	  }
	  StringBuffer sb=new StringBuffer();
	  for(int i=0;i<logs.size();i++){
		  sb.append(logs.get(i)+"\n");
	  }
	  logEditorPanel.setText(sb.toString().replaceAll("\n", "<br/>"));
  }
  
  private JSplitPane splitPane;
  private JSplitPane splitPaneV;
  private JEditorPane logEditorPanel;
  private  Stack<String> logs=new Stack<String>();;
  
  private Container con;
  private RemotePanel remotePanel;
  private LocalPanel localPanel;
  
  public static MainFrame mainFrame;
}
